/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 26727
 * Date: 2024-09-05
 * Time: 8:26
 */
class Solution4 {
    public String multiply(String num1, String num2) {
        // 1.准备工作
        int n = num1.length(),m = num2.length();
        int[] tmp = new int[n+m-1];
        char[] n1 = new StringBuilder(num1).reverse().toString().toCharArray();
        char[] n2 = new StringBuilder(num2).reverse().toString().toCharArray();

        // 2.遍历乘积
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < m; j++) {
                tmp[i+j] += (n1[i]-'0') * (n2[j]-'0');
            }
        }
        // 3.处理进位
        StringBuilder ret = new StringBuilder();
        int cur = 0, t = 0;
        while(cur < m+n-1 || t != 0) {
            if(cur < m+n-1) {
                t += tmp[cur++];
            }
            ret.append((char)((char)(t % 10)+'0'));
            t = t / 10;
        }
        // 4.处理前导0  注意这里的 while
        while(ret.length() > 1 && ret.charAt(ret.length()-1) == '0') {
            ret.deleteCharAt(ret.length()-1);
        }
        return ret.reverse().toString();
    }
}
